using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using CPGVertragsverwaltung.ErrorHandling;
using System.Diagnostics;
using System.Windows.Forms;

namespace CPGVertragsverwaltung.DatabaseAccess
{
    class DBUpdateBuilder
    {
        private String mTableName, mPrimaryKey, mPrimaryValue;
        private DataTable mSchemaTable = null;
        StringPairCollection mColumnPairCollection=new StringPairCollection();

        #region Properties
        public StringPairCollection ColumnPairCollection
        {
            get { return mColumnPairCollection; }
            set { mColumnPairCollection = value; }
        }
        #endregion


        public DBUpdateBuilder(String pTableName, String pPrimaryKey, String pPrimaryValue)
        {
            mTableName = pTableName;
            mPrimaryKey = pPrimaryKey;
            mPrimaryValue = pPrimaryValue;

            mSchemaTable = DBProvider.getSchemaTable(pTableName);          
        }

        public void addStringPair(StringPair pStringPair)
        {

            mColumnPairCollection.Add(pStringPair);
        }

        public String getUpdateString()
        {
            String result = "";
            StringBuilder mySBuilder = new StringBuilder();
            String UpdateLines = "";
            String PrimaryValue = "";

            //Error if mColumnsPair.Count =0
            if (mColumnPairCollection.Count == 0)
            {
                MessageHandling.showError("UpdateString kann nicht erstellt werden aufgrund fehlenden Daten!");
                Application.Exit();
            }

            //PrimaryValue with or without ' (Apostrophe)
            if (mSchemaTable.Columns[mPrimaryKey].DataType.ToString() == "System.Int32" || mSchemaTable.Columns[mPrimaryKey].DataType.ToString() == "System.Int64" || mSchemaTable.Columns[mPrimaryKey].DataType.ToString() == "System.Double")
            {
                PrimaryValue = mPrimaryValue;
            }
            else
            {
                PrimaryValue = "'" + mPrimaryValue + "'";
            }

            for (int i = 0; i < mColumnPairCollection.Count;i++)
            {
                StringPair myPair = mColumnPairCollection[i];
                UpdateLines += myPair.Key;
                UpdateLines += "=";

                //Check if Column exists
                if (mSchemaTable.Columns[myPair.Key] == null)
                {
                    MessageHandling.showError("Spalte " + myPair.Key + " von Tabelle " + mTableName + " kann nicht gelesen werden!", true);
                    Application.Exit();
                    return result;
                }

                //Value with or without ' (apostrophe)
                if (mSchemaTable.Columns[myPair.Key].DataType.ToString() == "System.Int32" || mSchemaTable.Columns[myPair.Key].DataType.ToString() == "System.Int64" || mSchemaTable.Columns[myPair.Key].DataType.ToString() == "System.Double" || mSchemaTable.Columns[myPair.Key].DataType.ToString() == "System.Boolean")
                {
                    if(myPair.Value=="")
                        myPair.Value="NULL";

                    UpdateLines += myPair.Value;
                }
                else
                {
                    //myPair.Value=myPair.Value.Replace(@"\",@"\\");
                    myPair.Value=myPair.Value.Replace("'","''");
                    UpdateLines += "'" + myPair.Value + "'";
                }

                 //Insert "," if it is not the last Item
                if (i < mColumnPairCollection.Count - 1)
                {
                    UpdateLines += ",";
                }
            }

            String[] agruments ={ mTableName, UpdateLines, mPrimaryKey, PrimaryValue };
            mySBuilder.AppendFormat("UPDATE {0} SET {1} WHERE {2}={3}",agruments);

            result = mySBuilder.ToString();
            return result;
        }
    }



    class StringPair
    {
        private String mKey, mValue;

        public StringPair(String pKey, String pValue)
        {
            mKey = pKey;
            mValue = pValue;
        }

        public String Key
        {
            get { return mKey; }
            set { mKey = value; }
        }

        public String Value
        {
            get { return mValue; }
            set { mValue = value; }
        }
    }

    class StringPairCollection : ArrayList
    {
        public StringPair this[int Index]
        {
            get { return (StringPair)base[Index]; }
            set { base[Index] = value; }
        }
    }
}
